<?php
/**
 * WpMemLog.php
 * 
 * This file contains the WPMemLog class to store log entries in memory.
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base.wplog
 */

/**
 * The WpLog interface to implement
 */
require_once(dirname(__FILE__).'/IWpLog.php');

/**
 * The WpLogBase class to extend
 */
require_once(dirname(__FILE__).'/WpLogBase.php');

/**
 * A class to keep the log entries in memory 
 * 
 * <code>
 * $l = new WpMemLog();
 * $l->logError('location1', 'user', 'a title', 'An error message', '0', 1);
 * $l->logAction('location2', 'user', 'a title', 'Done something', '0', 1);
 * $l->logAudit('location3', 'user', 'a title', 'Something important has been changed', '0');
 * 
 * print_r($l->grabNewest());
 * print_r($l->grabOldest());
 * print_r($l->grabNewest());
 * print_r($l->grabNewest());
 * 
 * //Will print the following
 * array('type' => 'audit','location' => 'location3','user' => 'user','date' => [the_date],'title' => 'a title',
 * 'body' => 'Something important has been changed','number' => '0')
 * array('type' => 'error','location' => 'location1','user' => 'user','date' => [the_date],'title' => 'a title',
 * 'body' => 'An error message','number' => '0')
 * array('type' => 'action','location' => 'location2','user' => 'user','date' => [the_date],'title' => 'a title',
 * 'body' => 'Done something','number' => '0')
 * null
 * </code>
 * 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base.wplog
 */
class WpMemLog extends WpLogBase implements IWpLog
{
	/**
	 * @var array An array of log entries which have not been grabbed yet
	 */
	private $_log = array();

	/**
   * Logs an entry
   * @param object $logObject The log entry to save
   */
	protected function _log2x($logObject)
	{
		$this->_log[] = $logObject;
	}
	
	/** 
	 * Retrieves the number of log entries
	 * @return int The number of log entries
	 */
	public function count()
	{
		return count($this->_log);
	}
	
	/**
	 * Returns the newest log and removes it from stack
	 * @return array The newest log object
	 */	
	public function grabNewest()
	{
		if (count($this->_log) < 1) {
			return null;
		}
		
		$objTemp = $this->_log[count($this->_log)-1];
		if (count($this->_log) > 1) {
			unset($this->_log[count($this->_log)-1]);
			$this->_log = array_values($this->_log);
		} else {
			unset($this->_log);
			$this->_log = array();
		}
		
		return $objTemp;
	}
	
	/**
	 * Returns the oldest log and removes it from stack
	 * @return array The oldest log object
	 */	
	public function grabOldest()
	{
		if (count($this->_log) < 1) {
			return null;
		}
		
		$objTemp = $this->_log[0];
		if (count($this->_log) > 1) {
			unset($this->_log[0]);
			$this->_log = array_values($this->_log);
		} else {
			unset($this->_log);
			$this->_log = array();
		}
				
		return $objTemp;
	}
}

/* End of file WpMemLog.php */
/* Location: ./wildpack/base/wplog/WpMemLog.php */ 
